package com.example.demo2.service.impl;

import com.example.demo2.mapper.CategoryMapper;
import com.example.demo2.mapper.EquipmentMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.utils.DataUtils;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.data.EquipmentDO;
import com.example.demo2.pojo.vo.EquipmentVO;
import com.example.demo2.service.EquipmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/** 
 * <br/>
 * Created by weidong on 2020/02/20
 */
@Service("equipmentService")
public class EquipmentServiceImpl implements EquipmentService {

	@Resource
	private EquipmentMapper equipmentMapper;
	@Resource
	private CategoryMapper categoryMapper;

	@Override 	public Result add(EquipmentVO equipmentVO) throws Exception {

		Integer count = null;
		try {
			count = equipmentMapper.add(equipmentVO);
		} catch (Exception e) {
			return Result.getFailure("编号必须唯一！");
		}
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getSuccess("添加失败");
		}
	}

	@Override
	public Result updateById(EquipmentVO equipmentVO) throws Exception {
		Integer count = equipmentMapper.updateById(equipmentVO);
		if (count > 0) {
			return Result.getSuccess("操作成功");
		}else{
			return Result.getSuccess("操作失败");
		}
	}
	
	@Override
	public Result findById(Integer id) throws Exception{
		EquipmentDO equipmentDO = equipmentMapper.findById(id);
		if (equipmentDO != null) {
			Result result = new Result();
			result.setMsg("操作成功");
			result.setData(equipmentDO);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public Result list(EquipmentVO equipmentVO) throws Exception{
		List<EquipmentDO> equipmentDOs = equipmentMapper.list(equipmentVO);
		if (equipmentDOs != null) {
			Result result = new Result();
			result.setMsg("查询成功");
			result.setData(equipmentDOs);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}
	int pageSize=10;
	int navigatePages=10;
	@Override
	public PageBean listPage(EquipmentVO equipmentVO) throws Exception{
		//设置分页数据大小
		equipmentVO.setPageSize(pageSize);
		//查询数目的总个数
		Integer count=equipmentMapper.count(equipmentVO);
		if(count==0){
			PageBean pageBean=new PageBean(null,pageSize,equipmentVO.getCurrentPage(),count,navigatePages);
			//数据库没有数据
			return pageBean;
		}

		//判断页码是否越界,如果页码大于最大值,返回最后一页数据
		//计算最大页码数
		int max= DataUtils.getPageCount(count,pageSize);
		if(equipmentVO.getCurrentPage()>max){
			equipmentVO.setCurrentPage(max);
		}

		//设置查询起始位置
		equipmentVO.setStartRecord(DataUtils.getStartRecord(equipmentVO.getCurrentPage(),pageSize));

		//调用mapper查询数据
		List<EquipmentDO> managerDOS=equipmentMapper.listPage(equipmentVO);
		PageBean pageBean=new PageBean(managerDOS,pageSize,equipmentVO.getCurrentPage(),count,navigatePages);

		return pageBean;
	}
	
	@Override
	public Result count(EquipmentVO equipmentVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteById(Integer id) throws Exception{
		Integer count = equipmentMapper.deleteById(id);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getFailure("操作失败");
		}
	}

    @Override
    public Result deleteByCategoryId(Integer id) throws Exception {
		Integer count=equipmentMapper.deleteByCategoryId(id);
		if (count > 0) {
			return Result.getSuccess("删除成功");
		}else{
			Integer integer = categoryMapper.deleteById(id);
			if (integer>0){
				return Result.getSuccess("删除成功");
			}
			else {
				return Result.getFailure("删除失败");
			}
		}
    }

}